﻿/* Copyright (C) Siemens AG, 2012. All Rights Reserved.
 * Author: Roland Eckl, Corporate Technology
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System.Collections.ObjectModel;
using System.Windows.Input;
using RvmShared.Base;


namespace RvmShared
{
    /// <summary>
    /// Simple test viewmodel, not based on RemotableViewModel, but implemented in the traditional way.
    /// </summary>
    public class TraditionalViewModel : ViewModel
    {
        #region Private Members

        /// <summary>
        /// Private member for Message.
        /// </summary>
        private string _message = string.Empty;

        /// <summary>
        /// Private member for ID.
        /// </summary>
        private string _id = string.Empty;

        /// <summary>
        /// Private member for SomeAction command.
        /// </summary>
        private ICommand _someAction = null;

        /// <summary>
        /// Counts how often the action was triggered.
        /// </summary>
        private int _actionCounter = 0;

        /// <summary>
        /// Private member for a collection of strings.
        /// </summary>
        private ObservableCollection<string> _nerds = new ObservableCollection<string>();

        #endregion

        #region Public Members

        /// <summary>
        /// Public Property for Message.
        /// </summary>
        public string Message
        {
            get { return _message; }
            set { _message = value; FireNotifyPropertyChanged("Message"); }
        }
        
        /// <summary>
        /// Public Property for ID.
        /// </summary>
        public string Id
        {
            get { return _id; }
            set { _id = value; FireNotifyPropertyChanged("Id"); }
        }

        /// <summary>
        /// Public Property for SomeAction command.
        /// </summary>
        public ICommand SomeAction
        {
            get
            {
                if (_someAction == null)
                    _someAction = new RelayCommand(SomeActionExecute, CanSomeActionExecute);
                return _someAction;
            }
            set { _someAction = value; }
        }

        /// <summary>
        /// Some collection of strings.
        /// </summary>
        public ObservableCollection<string> Nerds
        {
            get { return _nerds; }
            set { _nerds = value; FireNotifyPropertyChanged("Nerds"); }
        }

        private MinimalViewModel _nestedTraditional = null;

        /// <summary>
        /// Nested ViewModel implementing INotifyPropertyChanged.
        /// </summary>
        public MinimalViewModel NestedTraditional
        {
            get { return _nestedTraditional; }
            set
            {
                _nestedTraditional = value; 
                FireNotifyPropertyChanged("NestedTraditional");
            }
        }

        private MinimalRemotableViewModel _nestedSimple = null;

        /// <summary>
        /// Nested ViewModel derieved from RemotableViewModel.
        /// </summary>
        public MinimalRemotableViewModel NestedSimple
        {
            get { return _nestedSimple; }
            set 
            { 
                _nestedSimple = value;
                FireNotifyPropertyChanged("NestedSimple");
            }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Action that adds some text to the Message property.
        /// </summary>
        /// <param name="arg"></param>
        private void SomeActionExecute(object arg)
        {
            // This is an additional gate (optional)
            if (!CanSomeActionExecute(null))
                return;

            Message += " foo";
            if (_actionCounter < 3)
                Nerds.Add("bar");
            else if (_actionCounter < 6)
                Nerds.RemoveAt(0);

            _actionCounter++;
        }

        /// <summary>
        /// Checks wether SomeAction can still be executed.
        /// </summary>
        /// <param name="arg"></param>
        /// <returns>TRUE, if magic counter is below 2</returns>
        private bool CanSomeActionExecute(object arg)
        {
            return (_actionCounter < 7);
        }

        #endregion
    }
}
